package com.basic.boot.web.controller;

import com.basic.boot.web.service.FileService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author cherrish
 * @time 2019-04-02 10:18
 * @name FileController
 * @desc:
 */
@Controller
public class FileController {
    @RequestMapping("/view/file")
    public String file() {
        return "file";
    }

    @Resource
    private FileService fileService;

    @RequestMapping("/v1/api/file/upload")
    public String upload(@RequestParam(value = "fileUpload") MultipartFile uploadFile) {
        String json = "";
        try {
            Map result = fileService.uploadPicture(uploadFile);
            // 浏览器擅长处理json格式的字符串，为了减少因为浏览器内核不同导致的bug，建议用json
            json = new ObjectMapper().writeValueAsString(result);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return json;
    }
}
